/*
 * C4.13.3.c
 *
 *  Created on: 2012/12/11
 *      Author: ming
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rule_info.h"
#include "core.h"
#include "trace.h"
#include "TraceAst.h"

static const PSZ RuleID = "C4.13.3";

static const PSZ MESSAGE_ENG =
		"Evaluation value of the expression is dependent on the order of evaluation";
static const PSZ MESSAGE_JPN = "式の評価値は評価順序に依存している";
static const PSZ MESSAGE_CHS = "计算结果依赖于计算顺序";

static const U2 RANK = MSG_RANK_ERR;

static PRULEOUT pRuleHead;
static PRULEOUT pRuleTail;

static BOOL isIncDecMixed(AST exp) {
	if (exp <= 0) {
		return FALSE;
	}
	if (CExpression.isParentExp(exp)) {
		return TRUE;
	}
	return FALSE;
}

static BOOL isIncDecInFuncPara(AST exp) {
	U4 expKind = 0;
	AST childExp = 0;
	if (exp <= 0) {
		return FALSE;
	}
	expKind = CExpression.getOpKind(exp);
	if (CPostfixOpKind.isPostIncrement(expKind)
			|| CPostfixOpKind.isPostDecrement(expKind)
			|| CUnaryOpKind.isPreIncrement(expKind)
			|| CUnaryOpKind.isPreDecrement(expKind)) {
		return TRUE;
	}
	childExp = CExpression.getLeftNode(exp);
	if (isIncDecInFuncPara(childExp)) {
		return TRUE;
	}
	childExp = CExpression.getRightNode(exp);
	if (isIncDecInFuncPara(childExp)) {
		return TRUE;
	}
	return FALSE;
}

static BOOL isAssignNest(AST exp) {
	U4 expKind = 0;
	if (exp <= 0) {
		return FALSE;
	}
	if (!CExpression.isParentExp(exp)) {
		return FALSE;
	}
	exp = CExpression.parent_getExp(exp);
	if (exp <= 0) {
		return FALSE;
	}
	expKind = CExpression.getOpKind(exp);
	if (CExpressionOpKind.isAssignmentOp(expKind)) {
		return TRUE;
	}
	return FALSE;
}

/******************************************************************
 * main logic
 *******************************************************************/
static S4 RuleMain(U2 kind, AST index, U4 event) {
	INT result = RULE_RESULT_OK;
	PRULEOUT newNode = NULL;
	PRULEOUT tmpNode = NULL;
	PRULEOUT tmpNode1 = NULL;

	AST codeScp = 0;
	U4 expKind = 0;
	AST exp = 0;

	if (kind != TR_KIND_EXPRESSION) {
		return EXIT_SUCCESS;
	}

	if (!(event & EVT_EXPRESSION)) {
		return EXIT_SUCCESS;
	}

	expKind = CExpression.getOpKind(index);
	if (CPostfixOpKind.isPostIncrement(expKind)
			|| CPostfixOpKind.isPostDecrement(expKind)
			|| CUnaryOpKind.isPreIncrement(expKind)
			|| CUnaryOpKind.isPreDecrement(expKind)) {
		if (isIncDecMixed(index)) {
			result = RULE_RESULT_NG;
		}
	} else if (CPostfixOpKind.isFunctionCall(expKind)) {
		exp = CExpression.getRightNode(index);
		if (isIncDecInFuncPara(exp)) {
			result = RULE_RESULT_NG;
		}
	} else if (CExpressionOpKind.isAssignmentOp(expKind)) {
		if (isAssignNest(index)) {
			result = RULE_RESULT_NG;
		}
	} else {
		return EXIT_SUCCESS;
	}

	if (result == RULE_RESULT_OK) {
		return EXIT_SUCCESS;
	}

	/*re-checked?*/
	tmpNode = pRuleHead;
	while (tmpNode != NULL ) {
		if (tmpNode->indexInfo == index) {
			if (result > RULE_RESULT_OK) {
				result = RULE_RESULT_OK;
			} else {
				if (tmpNode == pRuleHead) {
					pRuleHead = pRuleHead->next;
					free(tmpNode);
				} else if (tmpNode == pRuleTail) {
					tmpNode1->next = NULL;
					pRuleTail = tmpNode1;
					free(tmpNode);
				} else {
					tmpNode1->next = tmpNode->next;
					free(tmpNode);
				}
			}
			break;
		}
		tmpNode1 = tmpNode;
		tmpNode = tmpNode->next;
	}

	/*make result struct*/
	if (result > RULE_RESULT_OK) {
		newNode = (PRULEOUT) malloc(sizeof(RULEOUT));
		if (pRuleHead == NULL ) {
			pRuleHead = newNode;
		}

		codeScp = CExpression.getCodeScope(index);
		newNode->indexInfo = index;
		newNode->fileIndex = CCodeScope.getFileIndex(codeScp);
		newNode->beginLine = CCodeScope.getBeginLine(codeScp);
		newNode->beginCol = CCodeScope.getBeginLineOffset(codeScp);
		newNode->endLine = CCodeScope.getEndLine(codeScp);
		newNode->endCol = CCodeScope.getEndLineOffset(codeScp);
		newNode->ruleID = RuleID;
		newNode->errRank = RANK;

		sprintf(newNode->msgBuf_eng, MESSAGE_ENG);
		sprintf(newNode->msgBuf_chs, MESSAGE_CHS);
		sprintf(newNode->msgBuf_jpn, MESSAGE_JPN);

		newNode->rfileIndex = 0;
		newNode->rlocLine = 0;
		newNode->next = NULL;

		if (pRuleTail != NULL ) {
			pRuleTail->next = newNode;
		}

		pRuleTail = newNode;
	}

	return EXIT_SUCCESS;
}

VOID C4_13_3_register(VOID) {
	PRULEMAN * pCurRule = &stRuleManager;
	PRULEMAN newRuleMan = (PRULEMAN) malloc(sizeof(RULEMAN));
	memset(newRuleMan, 0, sizeof(RULEMAN));
	newRuleMan->ruleID = RuleID;
	newRuleMan->ruleSwitch = OFF;
	newRuleMan->headOut = &pRuleHead;
	newRuleMan->tailOut = &pRuleTail;
	newRuleMan->ruleMain_cmd = NULL;
	newRuleMan->ruleMain_ast = RuleMain;
	newRuleMan->nextRule = NULL;
	if (stRuleManager == NULL ) {
		stRuleManager = newRuleMan;
	} else {
		while ((*pCurRule)->nextRule != NULL ) {
			pCurRule = &((*pCurRule)->nextRule);
		}
		(*pCurRule)->nextRule = newRuleMan;
	}
}

